{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "ba245e92-d4b8-44db-8c0e-ba00c2b3a81a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "551 394\n",
      "551 394\n"
     ]
    }
   ],
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "from IPython.display import clear_output\n",
    "\n",
    "import socket\n",
    "import sys\n",
    "import os\n",
    "import time\n",
    "\n",
    "video = cv2.VideoCapture(0)\n",
    "\n",
    "track_sock = './track.sock'\n",
    "arm_sock = './arm.sock'\n",
    "    \n",
    "sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)\n",
    "sock.connect(track_sock)\n",
    "sock2 = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)\n",
    "sock2.connect(arm_sock)\n",
    "\n",
    "t0 = 0\n",
    "\n",
    "try:\n",
    "    while True:\n",
    "        ret, img = video.read()\n",
    "        if not ret:\n",
    "            break\n",
    "\n",
    "        lower_hsv = np.array([100,43,46])\n",
    "        upper_hsv = np.array([124,255,255])\n",
    "\n",
    "        # 颜色遮罩\n",
    "        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)\n",
    "        mask = cv2.inRange(hsv, lower_hsv, upper_hsv)\n",
    "\n",
    "        # 锐化\n",
    "        sharpen_kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])\n",
    "        sharpen = cv2.filter2D(mask, -1, sharpen_kernel)\n",
    "\n",
    "        # 形态学变换\n",
    "        kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))\n",
    "        close = cv2.morphologyEx(sharpen, cv2.MORPH_OPEN, kernel, iterations=2)\n",
    "\n",
    "        # 提取边缘\n",
    "        cnts = cv2.findContours(close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n",
    "        cnts = cnts[0] if len(cnts) == 2 else cnts[1]\n",
    "\n",
    "        min_area = 2000\n",
    "        max_area = 20000\n",
    "        cnt = cnts[0]\n",
    "        for c in cnts:\n",
    "            area = cv2.contourArea(c)\n",
    "            # 按大小筛选\n",
    "            if area > min_area and area < max_area:\n",
    "                cnt = c\n",
    "                break\n",
    "        x,y,w,h = cv2.boundingRect(cnt)\n",
    "        cv2.putText(img, \"center: ({}, {})\".format(x+w/2, y+h/2), (x, y-20), cv2.FONT_HERSHEY_COMPLEX, 1, (218, 218, 3), 1)\n",
    "        cv2.rectangle(img, (x, y), (x + w, y + h), (218,218,3), 2)\n",
    "        clear_output(wait=True)\n",
    "        print(x,y)\n",
    "        if t0 == 0 and x >= 500 and y > 300 and y < 500:\n",
    "            sock.sendall('stop'.encode())\n",
    "            t0 = time.time()\n",
    "        if t0 > 0 and time.time() - t0 > 1:\n",
    "            sock2.sendall('{},{}'.format(x,y).encode())\n",
    "            print(x,y)  \n",
    "            break\n",
    "except:\n",
    "    pass\n",
    "finally:\n",
    "    video.release()\n",
    "    sock.close()\n",
    "    sock2.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aa1c1cfb-2845-4978-a436-5efec7bd6003",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
